<template>
  <Head />
  <div>
    <div class="MuiStack">
      <div class="gradientShadow"></div>
      <div class="headImageWrapper">
        <div class="headImage"></div>
      </div>
      <div class="headingContainer">
        <div class="MuiStack">
          <p class="headImagePara">
            {{ $t("ecologicalConsensusTips") }}
          </p>
          <van-button class="btn" @click="onContract">
            {{ $t("participateNow") }}
          </van-button>
        </div>
      </div>
    </div>

    <div class="MuiContainer">
      <div class="headingWrapper">
        <div class="heading">
          {{ $t("pledgeChain") }}
        </div>
        <div class="MuiGrid-container">
          <div class="totalSubWrapper">
            <li class="totalSubText">{{ $t("pledgeCertificate") }}:</li>
            <li class="totalSubs">
              {{ lendNum.type }}
            </li>
          </div>
          <div class="activeSubWrapper">
            <li class="totalSubText">{{ $t("pledgePeriod") }}:</li>
            <li class="totalSubs">
              {{ lendNum.day || 0 }} ({{$t('day')}})
            </li>
          </div>
        </div>
      </div>
    </div>

    <div class="subscriptionOptionWrapper">
      <span class="greenButtonWrapper">
        <div>
          {{ $t("pledgeRecord") }}
        </div>
      </span>
      <span class="greenButtonWrapper" @click="clickLendRedemption">
        <div>{{ $t("decompressRedeem") }}</div>
      </span>
    </div>
    <hr />
    <div class="content">
      <div class="item" v-for="(item, index) in pledgeRecord" :key="index">
        <div style="fontSize:14px">{{$t('pledgeQuantity')}}{{ item.num }}BT2 </div>
        <div>{{ FreeTime(item.createAt, "y/m/d") }} </div>
      </div>
      <div class="nodata" v-if="pledgeRecord.length == 0">{{ $t('noData') }}</div>
    </div>
  </div>
</template>

<script setup lang="ts">
// import { formatAddress } from "@/utils/kit";
import { Async_lendRecordList, Async_lendNum } from "@/server/api";
import { FreeTime } from "@/utils/tool";

interface IPledge {
  code: string
  contractAddress: string
  address: string
  coinType: string
  lendType: string
  num: Number
  price: Number
  totalMoney: Number
  cs: Number
  baseCs: Number
  createAt: Number
  updateAt: Number
  createBy: string
  hasRedemption: Number
  redemptionAt: Number
  redemptionHandleAt: Number
  redemptionSuccessAt: Number
  redemptionTxHash: string
}
let loading = ref(false)
let pledgeRecord = ref<IPledge[]>([])
let lendNum = ref({
  day: 0,
  type: '',
})
const getList = async () => {
  try {
    let { list }: any = await Async_lendRecordList({
      type: 'BT2',
      page: 0,
      size: 1000,
    });
    if (list) {
      pledgeRecord.value = list || [];
    }
  } catch (error) {
    console.error(error);
  }
};
const clickLendRedemption = async () => {
  router.push({
    path: 'redemption'
  });
  // try {
  //   const code = userStore().info.user?.code
  //   await Async_lendRedemption({
  //     code
  //   });
  // } catch (error) {
  //   console.error(error);
  // }
}
const getLendNum = async () => {
  try {
    let res: any = await Async_lendNum();
    if (res) {
      lendNum.value = res
    }
  } catch (error) {
    console.error(error);
  }
}
const router = useRouter();
const onContract = () => {
  router.push({
    path: 'contract'
  });
}
onMounted(() => {
  getList();
  getLendNum();
  loading.value = false;
});
</script>
<style scoped lang="scss">
$theme: #59e457;

.MuiStack {
  display: flex;
  flex-direction: column;
  position: relative;

  .gradientShadow {
    box-shadow: 0 -30px 70px 10px #59e457;
    position: absolute;
    top: 100px;
    width: 100%;
  }

  .headImageWrapper {
    background-color: #000;

    .headImage {
      background-color: #000;
      background-image: url("@/assets/image/aiInvestment.png");
      background-position-x: -10%;
      background-position-y: 96px;
      background-repeat: no-repeat;
      height: 336px;
    }
  }

  .headingContainer {
    position: absolute;
    top: 100px;
    width: 100%;
    margin-left: auto;
    box-sizing: border-box;
    margin-right: auto;
    display: block;
    padding-left: 16px;
    padding-right: 16px;

    .MuiStack {
      display: flex;
      flex-direction: column;
      align-items: flex-end;

      .headImagePara {
        word-wrap: break-word;
        color: #fff;
        font-family: Inter-Regular;
        font-size: 1.01rem;
        font-weight: 400;
        margin-top: 8px;
      }

      .btn {
        height: 40px;
        align-items: center;
        background: #59e457;
        border-color: #59e457;
        border-radius: 5px;
        display: inline-flex;
        justify-content: center;
        margin-top: 1.2rem !important;
        padding: 7px 28px;
        color: #000;
      }
    }
  }
}

.MuiContainer {
  width: 100%;
  margin-left: auto;
  box-sizing: border-box;
  margin-right: auto;
  display: block;
  padding-left: 16px;
  padding-right: 16px;

  .headingWrapper {
    background: $theme;
    border: 1px solid $theme;
    border-radius: 5px;
    margin-bottom: 20px;
    padding: 0.5rem 1.5rem;

    .heading {
      word-wrap: break-word;
      border-bottom: 0.5px solid #000;
      color: #0c0c0c;
      font-family: Ethnocentric;
      font-size: 1.25rem;
      line-height: 1.25rem;
      padding-bottom: 0.45rem;
      text-align: center;
      width: 100%;
      font-weight: bold;
    }

    .MuiGrid-container {
      box-sizing: border-box;
      display: flex;
      flex-flow: wrap;
      width: 100%;
      margin-top: 8px;

      .totalSubWrapper {
        box-sizing: border-box;
        margin: 0px;
        flex-direction: row;
        flex-basis: 50%;
        -webkit-box-flex: 0;
        flex-grow: 0;
        max-width: 50%;

        background-image: url("@/assets/image/maskGroup1.svg");
        background-position: 30% 100%;
        background-repeat: no-repeat;
        border-right: 0.5px solid #000;
      }

      .activeSubWrapper {
        box-sizing: border-box;
        margin: 0px;
        flex-direction: row;
        flex-basis: 50%;
        -webkit-box-flex: 0;
        flex-grow: 0;
        max-width: 50%;
        background-image: url("@/assets/image/maskGroup2.svg");
        background-position: 30% 100%;
        background-repeat: no-repeat;
      }

      .totalSubText,
      .totalSubs {
        word-wrap: break-word;
        color: #000;
        display: flex;
        justify-content: flex-end;
        text-decoration: none;
        padding: 8px 16px;
      }

      .totalSubText {
        font-family: Inter-Regular;
        font-size: 0.75rem;
        font-weight: 400;
        opacity: 0.5;
        padding-left: 10px;
        padding-right: 10px;
        text-align: right;
      }

      .totalSubs {
        font-family: Inter-Bold;
        font-size: 1.2rem;
        font-weight: 700;
        text-align: center;
      }
    }
  }

  .offersWrapper {
    align-items: center;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    justify-content: center;
    margin-top: 16px;

    .offer {
      background: #1a1d1a;
      border: 1px solid #1a1d1a;
      border-radius: 5px;
      flex: 1 1;
      padding: 20px 8px;
      text-align: center;
      height: 100%;

      .offerName {
        word-wrap: break-word;
        color: #fff;
        font-family: Inter-Bold;
        font-size: 15px;
        font-weight: 700;
        margin-bottom: 4px;
      }

      .offerPrice {
        word-wrap: break-word;
        color: #59e457;
        font-family: Inter-Regular;
        font-size: 13px;
        font-weight: 400;
      }
    }

    &.en {
      .offerPrice {
        font-size: 10px;
      }
    }
  }

}

.subscriptionOptionWrapper {
  background: #1a1d1a;
  border: 1px solid #1a1d1a;
  border-top-left-radius: 1.25rem;
  border-top-right-radius: 1.25rem;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  padding-top: 12px;
  padding-bottom: 12px;
  padding: 20px 20px 20px 20px;
  margin-top: 20px;

  .greenButtonWrapper {
    align-items: center;
    background: #1a1d1a;
    border-radius: 11px;
    display: inline-flex;
    gap: 10px;
    font-size: 12px;
    justify-content: center;
    padding: 12px 20px;
    color: #858585;

    &:first-child {
      background: #59e457;
      color: #000;
    }
    &:last-child {
      background: #59e457;
      color: #000;
    }
  }
}

hr {
  margin-left: auto !important;
  margin-right: auto !important;
  width: 95%;
}

.content {
  width: 100%;
  height: 100%;
  min-height: 300px;
  background: #1a1d1a;
  padding: 20px;
  box-sizing: border-box;

  .item {
    color: #fff;
    background: #444444;
    border-radius: 4px;
    padding: 10px 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    &:last-child {
      margin-bottom: 0px;
    }
  }
}

.nodata {
  display: flex;
  justify-content: center;
  color: #969799;
  font-size: 14px;
  align-items: center;
  margin-top: 40px;
}
</style>
